package org.azmat.mybatis.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.transaction.Transactional;
import org.azmat.mybatis.SQL.Entity.Book;
import org.azmat.mybatis.SQL.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/books")
    public ModelAndView findAllBooks() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("books", bookService.selectAllBooks());
        modelAndView.setViewName("books");
        return modelAndView;
    }

    // 查找指定编号的图书
    @GetMapping("/books/{id}")
    public ModelAndView findById(@PathVariable("id") int id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("book", bookService.selectByIdBook(id));
        modelAndView.setViewName("bookId");
        return modelAndView;
    }

    // 添加图书
    @PostMapping("/books/add")
    public ModelAndView addBook(Book book) {
        ModelAndView modelAndView = new ModelAndView();
        bookService.insertBook(book);
        modelAndView.setViewName("redirect:/books");
        return modelAndView;
    }

    // 添加书视图界面
    @GetMapping("/books/add")
    public ModelAndView addBookPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("bookAdd");
        return modelAndView;
    }

    // 根据编号删除书
    @GetMapping("/books/delete/{id}")
    public ModelAndView deleteBook(@PathVariable("id") int id) {
        ModelAndView modelAndView = new ModelAndView();
        bookService.deleteBook(id);
        modelAndView.setViewName("redirect:/books");
        return modelAndView;
    }

    // 修改书视图界面
    @GetMapping("/books/update/{id}")
    public ModelAndView updateBookPage(@PathVariable("id") int id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("book", bookService.selectByIdBook(id));
        modelAndView.setViewName("bookUpdate");
        return modelAndView;
    }

    // 修改书
    @PostMapping("/books/update")
    public ModelAndView updateBook(Book book) {
        ModelAndView modelAndView = new ModelAndView();
        bookService.updateBook(book);
        modelAndView.setViewName("redirect:/books");
        return modelAndView;
    }

    // 动态查询图书
    @GetMapping("/books/search")
    public ModelAndView searchBooks(Book book) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("books", bookService.searchBooks(book));
        modelAndView.setViewName("books");
        return modelAndView;
    }

    // 分页查询
    @GetMapping("/books/page")
    public ModelAndView pageBooks(@RequestParam("page") int page, @RequestParam("size") int size) {
        PageHelper.startPage(page, size,"id desc");
        ModelAndView modelAndView = new ModelAndView();
        List<Book> books = bookService.selectAllBooks();
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        modelAndView.addObject("books",pageInfo);
        modelAndView.setViewName("books");
        return modelAndView;
    }

    // 批量删除
    @GetMapping("/delete/books")
    public ModelAndView deleteBooks(int[] id) {
        bookService.deleteBooks(id);
        ModelAndView modelAndView = new ModelAndView();
        // 删除成功后跳转到图书列表界面
        modelAndView.setViewName("redirect:/books");
        return modelAndView;
    }
}
